<?
/**
 * Category Ids Management Functions
 */
(basename($_SERVER['PHP_SELF']) == basename(__FILE__)) && die('Access Denied');
requireWebApp();

/**
 * Get menu bar local to this applet
 * @param string $currentPage
 * @param bool canDelete
 * @return string
 */
function getMenuBar($currentPage = null, $canDelete = false) {
	global $_APP;

	// All possible links
	$add = new menuLink($_APP->html->getActionQs(ACTION_ADD),
	                    'images/add32.png',
	                    OPT_ADD,
	                    ($currentPage == OPT_ADD));
	$back = new menuLink($_APP->html->getActionQs(ACTION_LIST),
	                     'images/left.png',
	                     OPT_BACK);
	$del  = new menuLink($_APP->html->getActionQs(ACTION_DEL),
	                     'images/trash32.png',
	                     OPT_DEL,
	                     ($currentPage == OPT_DEL));
	$dn  = new menuLink($_APP->html->getActionQs(ACTION_LIST)
	                    . '&' . WA_QS_DOWNLOAD . '=yes',
	                    'images/download32.png',
	                    OPT_DOWN);
	$edit = new menuLink($_APP->html->getActionQs(ACTION_EDIT),
	                     'images/edit32.png',
	                     OPT_EDIT,
	                     ($currentPage == OPT_EDIT));
	$view = new menuLink($_APP->html->getActionQs(ACTION_VIEW),
	                     'images/view32.png',
	                     OPT_VIEW,
	                     ($currentPage == OPT_VIEW));
	$sm = new menuBar();
	// different pages get different options
	switch($currentPage) {
		case OPT_ADD:
			$sm->addToRight($back);
			$sm->addToRight($add);
		break;
		case OPT_LIST:
		case OPT_BACK:
			$sm->addToRight($dn);
			$sm->addToRight($add);
		break;
		default:
			$sm->addToRight($back);
			$sm->addToRight($edit);
			$sm->addToRight($view);
			if ($canDelete === true) { $sm->addToRight($del); }
			$sm->addToRight($add);
		break;
		}
    return $sm->get();
	}

/**
 * Get main and local menu bars.
 * @param string $currentPage
 * @param bool canDelete
 * @return string
 */
function getMenuBars($currentPage = null, $canDelete = false) {
	return getMenuBarMain(OPT_ADMIN) . getMenuBar($currentPage, $canDelete);
	}

/**
 * Render page and process request to add item
 * @return void
 */
function getPageAdd() {
	// requirements
	global $_APP;

	// objects we need
	$dbo = getDbo('Category');
	$dbo->resetProperties();
	$dbo->populateFromPost();

	// Set up the html template
	$_APP->html->setVar('item', $dbo);

	// Has user confirmed?
	if (!$_APP->html->hasConfirmed()) {
		die($_APP->html->getTemplate('add.php'));
		}

	// Submit the changes
	$dbo->populated = true;

	// do it
	$rc = $dbo->add();
	if (!($rc === true)) {
		$_APP->html->addError($rc);
		die($_APP->html->getTemplate('add.php'));
		}

	// Log
	$m = 'add category(' . $dbo->Id() . ')';
	$_APP->log->log_audit($m);

	// View the results
	$rc = $_APP->html->setCrumb(WA_QS_ID, $dbo->getProperty('Id'));

	die(getPageView());
	}

/**
 * Render page and process requests to delete an item
 * @return void
 */
function getPageDelete() {
die('not done yet!' . __LINE__);
	// requirements first
	global $_APP;

	// pull out the id
	$id = $_APP->html->getCrumb(WA_QS_ID);
	if (!is_numeric($id) or $id < 1) {
		die('FATAL ERROR: id not specified');
		}

	// Look up the object
	$dbo = getDbo('Source');
	$dbo->resetProperties();
	if ($dbo->populateFromDb($id) === false) {
		die($_APP->html->getPageError('Failed to locate the item.'));
		}
	$_APP->html->setVar('item', $dbo);

	// callback page
	$callback = 'getPageView';

	// Has the user confirmed?
	if (!$_APP->html->hasConfirmed()) {
		die($_APP->html->getTemplate('delete.confirm.php'));
		}

	// Do the nasty
	$rc = $dbo->delete();
	if (!($rc === true)) {
		$_APP->html->addError($rc);
		die($callback());
		}

	// Log it
	$m = 'delete source id(' . $dbo->Id() . ')';
	$_APP->log->log_audit($m);

	// Strip the delete bread crumbs
	$_APP->html->deleteCrumb(WA_QS_ACTION);
	$_APP->html->deleteCrumb(WA_QS_ID);

	// Reload the list page
	die(getPageList());
	}

/**
 * Render page and process request to edit item
 * @return void
 */
function getPageEdit() {
	// requirements first
	global $_APP;

	// pull out the id
	$id = $_APP->html->getCrumb(WA_QS_ID);
	if (!is_numeric($id) or $id < 1) {
		die('FATAL ERROR: id not specified');
		}

	// Look up the object
	$dbo = getDbo('Category');
	$dbo->resetProperties();
	if ($dbo->populateFromDb($id) === false) {
		die($_APP->html->getPageError('Failed to locate the item.'));
		}

	// Apply changes from user input
	$dbo->populateFromPost();

	// Set up the html template
	$_APP->html->setVar('item', $dbo);

	// Has the user confirmed?
	if (!$_APP->html->hasConfirmed()) {
		die($_APP->html->getTemplate('edit.php'));
		}

	// Save the changes
	$rc = $dbo->update();
	if (!($rc === true)) {
		$_APP->html->addError($rc);
		die($_APP->html->getTemplate('edit.php'));
		}

	// Log
	$m = 'edit category id(' . $dbo->Id() . ') properties';
	$_APP->log->log_audit($m);

	// View the results
	die(getPageView());
	}

/**
 * Generate data and load list template when listing objects
 * @return void
 */
function getPageList() {
	// requirements
	global $_APP;

	// obtain object and set list properties
	$dbo = getDbo('Category');
	if ($_APP->html->downloadRequested()) {
		$props = getListDownloadProperties($dbo);
		} else {
		$props = array('Id', 'Tag', 'Color', 'Desc');
		}
	$_APP->html->setPropertiesList($props);

	// search properties
	foreach($dbo->getPropertyList() as $prop) {
		$_APP->html->addSearchProperty($prop);
		}

	// Default search property and order
	$_APP->html->setSearchPropertyDefault('Tag');
	$_APP->html->setSearchOrderDefault('Tag');


	// Reset search and apply user input
	$dbo->searchReset();
	// apply user input
	$_APP->html->setUserInput($dbo);

	// Do the query
	$_APP->html->doQueryList($dbo);

	// Take care of download requests
	if ($_APP->html->downloadRequested()) {
		$h = array();
		// first line contains field names
		$h[] = '"' . implode('","', $props) . '"';
		foreach($_APP->html->searchResults as &$row) {
			$h[] = '"' . implode('","', $row) . '"';
			}
		$txt = implode("\r\n",$h) . "\r\n";
		unset($h);
		die($_APP->html->uploadContent($txt, 'category-list.csv'));
		}

	// set up the html template
	$_APP->html->setVar('list', $dbo);
	die($_APP->html->getTemplate('list.php'));
	}

/**
 * Generate data and load list template when viewing items
 * @return void
 */
function getPageView() {
	// requirements
	global $_APP;
	requireWebApp();

	// get objects we need
	$dbo = getDbo('Category');

	// populate the item
	$id = $_APP->html->getCrumb(WA_QS_ID);
	$dbo->resetProperties();
	$rc = $dbo->populateFromDb($id);
	if (!($rc === true)) {
		die($_APP->html->getPageError('Failed to locate the item'));
		}

	// set return url
	$_APP->returnUrlSet($_APP->html->getActionQs(ACTION_VIEW));

	// set up the html template
	$_APP->html->setVar('item', $dbo);
	die($_APP->html->getTemplate('view.php'));
	}

?>
